package com.yiyi.credit.manage.abutment.common.mail;

import com.yiyi.credit.manage.abutment.common.bean.Response;
import com.yiyi.credit.manage.abutment.common.exceptions.ServiceException;
import com.yiyi.credit.manage.abutment.common.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;


@Service
public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    @Autowired
    private JavaMailSender javaMailSender;


    public Response sendMail(String from, String to, String subject, String text) {
        if (StringUtils.isBlank(from)) {
            logger.error("发件人不能为空");
            return new Response("50500", "发件人不能为空");
        }
        if (StringUtils.isBlank(to)) {
            logger.error("收件人不能为空");
            return new Response("50500", "收件人不能为空");
        }
        if (StringUtils.isBlank(text)) {
            logger.error("邮件内容不能为空");
            return new Response("50500", "邮件内容不能为空");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try {
            javaMailSender.send(message);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("邮件发送失败, ：{}，发件人：{}, 收件人：{}, \n邮件主题：{}, \n邮件内容：{}", e.getMessage(), from, to, message.getSubject(), message.getText());
            return new Response("50502", "发送邮件失败");
        }
        return new Response("10000", "发送邮件成功");
    }


    public Response sendMail(String from, String to, String subject, String text, File... files) {
        if (StringUtils.isBlank(from)) {
            logger.error("发件人不能为空");
            return new Response("50500", "发件人不能为空");
        }
        if (StringUtils.isBlank(to)) {
            logger.error("收件人不能为空");
            return new Response("50500", "收件人不能为空");
        }
        if (StringUtils.isBlank(text)) {
            logger.error("邮件内容不能为空");
            return new Response("50500", "邮件内容不能为空");
        }
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(text);

            if (null != files && files.length > 0) {
                for (File file : files) {
                    mimeMessageHelper.addAttachment(DateUtil.dateToString("yyyyMMdd", new Date()) + ".xls", file);
                }
            }
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            try {
                logger.error("邮件发送失败, ：{}，发件人：{}, 收件人：{}, \n邮件主题：{}", e.getMessage(), from, to, mimeMessage.getSubject());
            } catch (MessagingException e1) {
                throw new ServiceException("带附件的邮件发送失败");
            }
            return new Response("50502", "发送邮件失败");
        }
        return new Response("10000", "发送邮件成功");
    }

}
